#!/usr/bin/lua50

--[[
        generate boot C files
        See Copyright Notice in "invoke.h"
]]

boot, e = loadfile("boot.lua")

if not boot then
	error(e)
end

local sboot = string.dump(boot)
local out = io.output()
local len = string.len(sboot)

print([[
/*
* invoke boot bytecode sequence
* See Copyright Notice in "invoke.h"
*/
]])

if arg[1] == "-h" then
print([[
#ifndef BOOT_H
#define BOOT_H

#define MAX_BOOT ]] .. len  .. [[


extern const char boot_bcodes[MAX_BOOT];

#endif
]])
	return
end

print([[
#include "boot.h"

const char boot_bcodes[MAX_BOOT] = { ]])

local b
out:write("\t")
for i=1,len do
	b = string.byte(sboot, i)
	out:write(string.format("0x%02x", b))
	if i < len then
		out:write(", ")
	end
	if math.mod(i, 16) == 0 then
		out:write("\n\t")
	end
end

print()
print("};")
